import cv2

def concatenate_videos_any_fps(videoA_path, videoB_path, output_path, target_fps=30):
    # 读取视频
    capA = cv2.VideoCapture(videoA_path)
    capB = cv2.VideoCapture(videoB_path)
    
    # 获取视频A的帧（跳过最后一帧）
    framesA = []
    while capA.isOpened():
        ret, frame = capA.read()
        if not ret:
            break
        framesA.append(frame)
    if framesA:
        framesA = framesA[:-1]  # 删除最后一帧
    
    # 获取视频B的所有帧
    framesB = []
    while capB.isOpened():
        ret, frame = capB.read()
        if not ret:
            break
        framesB.append(frame)
    
    # 统一输出尺寸（以第一个视频的尺寸为准）
    if framesA:
        height, width = framesA[0].shape[:2]
    else:
        height, width = framesB[0].shape[:2]
    
    # 写入新视频（强制统一帧率）
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    out = cv2.VideoWriter(output_path, fourcc, target_fps, (width, height))
    
    for frame in framesA + framesB:
        out.write(frame)
    
    capA.release()
    capB.release()
    out.release()

# 示例调用
concatenate_videos_any_fps(r"C:\Users\86152\Downloads\苹果树6-4.mp4", r"C:\Users\86152\Downloads\苹果树7-1.mp4", r"C:\Users\86152\output_wan_video\output.mp4", target_fps=30)